#include "ModelData.h"

//-------------------------------------------------------------------------------------------------------
ModelData::ModelData(ID3D11Device* device, const string& modelFilename)
{
	bool result;

	result = Initialise(device, modelFilename);

	if (!result)
		MessageBox(NULL, L"Unable to create model!", L"Error", MB_OK);

	m_indexCount = m_ModelTypeArray.size();
}

//-------------------------------------------------------------------------------------------------------
ModelData::~ModelData()
{ }

//-------------------------------------------------------------------------------------------------------
void ModelData::Shutdown()
{
	ReleaseCOM(m_VertexBuffer);
	ReleaseCOM(m_IndexBuffer);

	vector<ModelType*>::iterator it = m_ModelTypeArray.begin();
	for (it; it != m_ModelTypeArray.end(); ++it)
	{
		delete (*it);
	}
}

//-------------------------------------------------------------------------------------------------------
bool ModelData::Initialise(ID3D11Device* device, const string& modelFilename)
{
	bool result;

	// Generate model data based on a model text file.

	char filename[128];
	memset(&filename, 0, (sizeof(char) * 64));	// Initialise to zeros.

	for (unsigned int i = 0; i < modelFilename.size(); ++i)	// Copy in string filename.
	{
		filename[i] = modelFilename[i];
	}

	const int size = modelFilename.size();

	filename[size] = '\0';	// Add null-terminator.

	result = ModelUtil::LoadModel(filename, m_ModelTypeArray);
	if (!result)
		return false;

	// Create vertex/index buffer and AABB.

	m_VertexBuffer = ModelUtil::InitialiseVertexBuffer(device, m_ModelTypeArray, m_AABB);

	m_IndexBuffer  = ModelUtil::InitialiseIndexBuffer(device, m_ModelTypeArray);

	return true;
}

//-------------------------------------------------------------------------------------------------------
ID3D11Buffer* ModelData::GetVertexBuffer() const
{
	return m_VertexBuffer;
}

//-------------------------------------------------------------------------------------------------------
ID3D11Buffer* ModelData::GetIndexBuffer() const
{
	return m_IndexBuffer;
}

//-------------------------------------------------------------------------------------------------------
XNA::AxisAlignedBox* ModelData::GetAABBp()
{
	return &m_AABB;
}

//-------------------------------------------------------------------------------------------------------
unsigned int ModelData::GetIndexCount() const
{
	return m_indexCount;
}